 <?php
include("config.php");
if (!isset($_SESSION["user"]) || $_SESSION["user"] == "") header('Location: index.php');
include("header.php");
include("menu.php");

$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
if ($db_www->isConnected()) {	
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	
	if (!empty($minfo)) {
		$skola_query = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $skola_query);
		$sinfo = $db_www->returnInfo($skola_query);	
        if (!empty($sinfo)) {
			$dskola=$sinfo["skola"];
		} else {
			$dskola=0;
		}
		
		$ns_query2 = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $ns_query2);
		$ns = $db_www->returnList($ns_query2);	
                //echo $ns_query2; 		
		
		$mokejimai_query = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $mokejimai_query);
		$mokejimai = $db_www->returnList($mokejimai_query);
		//echo $mokejimai_query;
	} else {
		die("Nerasta imone");
	}
	
} else {
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}

if (!empty($mokejimai)) {

$gautimokejimai = '';
$gautimokejimai .= '<table cellspacing="0" cellpadding="5" align="left" class="innercont3">';
$gautimokejimai .= '<tr>';
if (!isset($oldstyle) || $oldstyle != "1") {
	$td="<td>&nbsp;</td>";
	$gautimokejimai .= '<td class="th3">Gauti mokėjimai</td>';
} else {
	$td="";
}
$gautimokejimai .='<th>Data</th><th>Numeris</th><th>Suma</th></tr>';
foreach ($mokejimai as $mokejimas) {
	$gautimokejimai .=  "<tr>".$td."<td>".$mokejimas["i04_op_data"]."</td>";
	$gautimokejimai .=  "<td>".$mokejimas["i04_dok_nr"]."</td>";
	$gautimokejimai .=  "<td>".$mokejimas["i04_suma"]."</td></tr>";
}

$gautimokejimai .= '</td></tr>';
$gautimokejimai .= '</table>';


} else {
	$gautimokejimai = "";
}





?>
<table class="innercont" cellspacing="0" cellpadding="5">
<tr><td valign="top">
<table cellspacing="0" cellpadding="0" border="0"><tr><td>
<?php
if (isset($oldstyle) && $oldstyle == "1") {
?>
<table width="100%" cellspacing="0" cellpadding="5" border = "0" style="border-collapse:collapse;">
<tr><td class="title">Aktyvūs dokumentai</td></tr>
</table>

<?php
}
if (!empty($ns)) {
?>
</td></tr><tr><td>
<br><br>
<table cellspacing="0" cellpadding="5" align="left" class="innercont3">
<tr>
<?php
if (!isset($oldstyle) || $oldstyle != "1") {
	$td="<td>&nbsp;</td>";
?>
<td class="th3">Aktyvūs dokumentai</td>
<?php
} else {
	$td="";
}
?>
<th>Data</th><th>Numeris</th><th>Apmokėti iki</th><th>Dienos</th><th>Suma</th><th>Neapmokėta suma</th><th>Avansinis likutis</th></tr>
<?php
foreach ($ns as $account) {
	//if ($account["i44_suma_DB"] != $account["i44_suma_CR"]) {
		
		$ns_q = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $ns_query3);
		$ns_q = str_replace("<dok_nr>", $account["i44_dok_nr"], $ns_q);
		$ns1 = $db_www->returnInfo($ns_q);	
		//echo $ns_q;
		if ($ns1["dienos"] >=0 ){
			$color = "green";
		} else {
			$color = "red";
		} 
		$likutis = $account["db"]-$account["cr"];
		echo "<tr>".$td."<td>".$ns1["I44_DATA_DOK"]."</td>";
		echo "<td>".$account["i44_dok_nr"]."</td>";
		echo "<td>";
		if ($likutis >= 0) {
			echo $ns1["I44_DATA_MOK"];
		}
		echo "</td>";
		echo "<td style='color:".$color.";'>";
		if ($likutis >= 0) {
			echo $ns1["dienos"];
		}
		echo "</td>";
		echo "<td>";
		if ($likutis >= 0) {
			echo $account["db"];
		}
		echo "</td>";
		echo "<td>";
		if ($likutis >= 0) {
			echo $likutis;
		}
		echo "</td>";
		echo "<td>";
		if ($likutis < 0) {
			echo ($likutis*(-1));
		}
		echo "</td></tr>";
		
		
	//}
}
?>
<tr><td>&nbsp;</td></tr>
</table>
</td></tr><tr><td>
<?php
} 
if (!isset($oldstyle) || $oldstyle != "1") {
	echo $gautimokejimai;	
}
?>
</td></tr></table>
</td><td  width="340" valign="top">
<br><br>
<table class="payments" cellspacing="0" cellpadding="5">
<tr>
<td class="paymentsth borderbottom">Kredito limitas:</td><td class="borderbottom borderlr">&nbsp;</td><td class="borderbottom center"><?php echo $minfo["n08_suma_wk_limit"];?></td>
</tr>
<?php
if ($dskola>= 0) {
	if ($dskola == 0)
		$tskola = "";
	else
		$tskola = $dskola;
?>
<tr>
<td class="paymentsth borderbottom">Skola:</td><td class="borderbottom borderlr" style="color:red;"><?php echo $tskola;?><td class="borderbottom center" >&nbsp;</td></td>
</tr>
<?php
} else {
?>
<tr>
<td class="paymentsth borderbottom">Likutis:</td><td class="borderbottom borderlr" >&nbsp;</td><td class="borderbottom center"><?php echo abs($dskola);?></td>
</tr>
<?php
}
if ($kreditas == "1") {

		$lastaccdate_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $lastaccdate_query);
		$lastacc = $db_www->returnInfo($lastaccdate_query);

		if (!isset($lastacc["I31_OP_DATA"])) {
			$pask = "2000.01.01";
		} else {
			$pask = $lastacc["I31_OP_DATA"];
		}
		
		$k_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $k_query);
		$kinfo = $db_www->returnList($k_query);
		$numbers = "'333'";
		foreach ($kinfo as $card) {
			$numbers .= ",'" . trim($card["N51_PAV"]) . "'";
		}

		$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
		if (!$db_rdata->isConnected()) {	
			die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
		}
		
		$today = date("Y-m-d");
		
		$imusedcredit_query = str_replace("<kodas>", $numbers, $imusedcredit_query);		
		$imusedcredit_query = str_replace("<from>", str_replace(".","-",$pask), $imusedcredit_query);
		$imusedcredit_query = str_replace("<to>", $today, $imusedcredit_query);
		$info = $db_rdata->returnInfo($imusedcredit_query);		
		//echo $imusedcredit_query;
		
		
	    
?>
<tr>
<td class="paymentsth borderbottom">Einamojo mėnesio pirkimai:</td><td class="borderbottom borderlr" style="color:red;"><?php if($info["Kvitosuma"] == null) echo "0.00"; else echo sprintf("%01.2f",$info["Kvitosuma"]);?><td class="borderbottom center">&nbsp;</td></td>
</tr>
<tr>
<?php 
if($info["Kvitosuma"] == null){
	$nep_cred = $minfo["n08_suma_wk_limit"]-$dskola;
} else {
	$nep_cred = $minfo["n08_suma_wk_limit"]-$dskola -$info["Kvitosuma"];
}
?>
<td class="paymentsth borderbottom"><?php if ($nep_cred >= 0) {echo "Nepanaudotas"; } else { echo "Viršytas";}?> kreditas:</td><td class="borderbottom borderlr" >&nbsp;</td><td class="borderbottom center"><?php echo sprintf("%01.2f",$nep_cred);?></td>
</tr>
<?php
} else {
?>
<tr>
<td class="paymentsth borderbottom">Nepanaudotas kreditas:</td><td class="borderbottom borderlr" >&nbsp;</td><td class="borderbottom center"><?php echo sprintf("%01.2f",$minfo["n08_suma_wk_limit"]-$dskola);?></td>
</tr>
<?php
}

if (!isset($_REQUEST['data'])) {
	$data = lastOfPrevMonth();
} else {
	$data = $_REQUEST['data'];
}
if (!isset($oldstyle) || $oldstyle != "1") {
?>
<tr>
<td class="paymentsth borderbottom" colspan="3" align="right">
<script>
  $(function() {
    $( ".dt" ).datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true,
	  dateFormat: "yy.mm.dd",
	  regional: "lt"
    });
  });
  </script>
<form method="post" action="act.php" style="margin:0px;">	
<input class="dt" type='text' name="data" value="<?php echo $data;?>"><br>
<input class="but bgfilter" type = "submit" value = "Suderinimo aktas">
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
if (isset($oldstyle) && $oldstyle == "1") {
?>
<br><br>
<form method="post" action="act.php">	
<input type = "submit" value = "Suderinimo aktas">
<input class="dt" type='text' name="data" value="<?php echo $data;?>">
</form>
<?php
}
?>
</td></tr></table>
<?php
if (isset($oldstyle) && $oldstyle == "1") {
?>
<table class="innercont" cellspacing="0" cellpadding="5">
<tr><td class="title">Gauti mokėjimai</td></tr>
<tr><td>
<?php
echo $gautimokejimai;
}
include("footer.php");
?>